package combofs.demo;

import com.google.common.io.Files;
import keter.combofs.api.s3.ComboS3;
import keter.combofs.model.ComboFile;
import keter.combofs.model.FileMeta;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.ClassPathResource;

@SpringBootApplication
public class ComboS3Application implements CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(ComboS3Application.class);

    @Autowired
    private ComboS3 fs;

    @Override
    public void run(String... args) throws Exception {
        // 测试用的本地文件
        java.io.File f = new ClassPathResource("application.properties").getFile();
        // 保存文件：只保存文件流
        ComboFile file = fs.save(Files.toByteArray(f));
        logger.info("读取文件: id={}, uri={}",fs.getComboFile(file.getId()).getId(),fs.getComboFile(file.getId()).getUri());
//       Assert.isTrue("ee3cf2fa71519d8286e1b2032add7547".equals(file.getMD5()),"比对MD5");

        logger.info("读取文件流：性能最好!");
        Validate.notNull(fs.getBytes(file.getId()));
        logger.info("读取文件完整信息: id={}",fs.getComboFile(file.getId()).getId());

        // 保存文件：保存流和文件元数据
        // 构建元数据
        FileMeta meta =  FileMeta.build()
                .add("kaka","keke")
                .add("haha","hehe");
        file = fs.save(Files.toByteArray(f) ,meta);
        logger.info("读取文件:{}",fs.getComboFile(file.getId()));
        logger.info("文件删除：");
        Long id = 1234L;
        fs.delete(id);
    }

    public static void main(String[] args) {
        SpringApplication.run(ComboS3Application.class, args);
    }

}
